package com.xinsoft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.common.exception.BDException;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.QuaPatrolDTO;
import com.xinsoft.entity.dto.QuaPatrolParam;
import com.xinsoft.entity.po.*;
import com.xinsoft.entity.vo.QuaPatrolVO;
import com.xinsoft.service.*;
import com.xinsoft.mapper.QuaPatrolMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author lijie
* @description 针对表【qua_patrol(质量巡检)】的数据库操作Service实现
* @createDate 2023-08-02 09:42:09
*/
@Service
public class QuaPatrolServiceImpl extends ServiceImpl<QuaPatrolMapper, QuaPatrol>
    implements QuaPatrolService{

    @Resource
    QuaDocumentService documentService;

    @Resource
    SysDocumentNumService sysDocumentNumService;

    @Resource
    QuaPatrolMapper quaPatrolMapper;

    @Resource
    QuaPatrolDetailService detailService;

    @Resource
    SysUserService userService;

    @Resource
    SysDeptService deptService;

    @Resource
    SysFileHisService fileHisService;

    @Resource
    QuaPatrolStandardService standardService;

    @Override
    public Object findPageListByParam(QuaPatrolParam param) {
        //分页
        Page<QuaPatrol> page = new Page<QuaPatrol>(param.getCurrentPage(), param.getPageSize());
        IPage<QuaPatrol> resultPage = quaPatrolMapper.findListByParam(page, param);
        //返回值
        return resultPage;
    }

    @Override
    public void tempSave(QuaPatrolDTO quaPatrolDTO) {
        // 暂存有新增和编辑场景
        if (quaPatrolDTO.getId() == null) {
            quaPatrolDTO.setPatrolStatus(Const.UPKEEP_STATUS_TEMPSAVE);
            sysDocumentNumService.incrementLastSerialByDocumentId(Const.DOCUMENT_ID.QUA_PATROL_ORDER_DOCUMENT);
            quaPatrolMapper.insert(quaPatrolDTO);
            // 解析执行设备项目关系表
            if (quaPatrolDTO.getQuaPatrolDetailList().size() > 0) {
                this.dealList(quaPatrolDTO);
            } else {
                throw new BDException("操作失败，不包含设备巡检标准集合！");
            }
            // 巡检文件操作
            this.saveDocumentPatrolList(quaPatrolDTO);
            // 巡检文件操作
            this.saveDocumentPatrolResultList(quaPatrolDTO);
        } else {
            if (quaPatrolDTO.getQuaPatrolDetailList().size() > 0) {
                quaPatrolDTO.setPatrolStatus(Const.UPKEEP_STATUS_TEMPSAVE);
                detailService.remove(new QueryWrapper<QuaPatrolDetail>().eq("patrol_id", quaPatrolDTO.getId()));
                this.dealList(quaPatrolDTO);
            } else {
                throw new BDException("操作失败，不包含巡检标准集合！");
            }
            quaPatrolMapper.updateById(quaPatrolDTO);
            // 质量巡检-拍照签到附件
            documentService.remove(new QueryWrapper<QuaDocument>().eq("qua_id", quaPatrolDTO.getId()).eq("qua_type",Const.DOCUMENT_QUA_TYPE.QUA_PATROL));
            this.saveDocumentPatrolList(quaPatrolDTO);
            // 质量巡检-巡检结果附件
            documentService.remove(new QueryWrapper<QuaDocument>().eq("qua_id", quaPatrolDTO.getId()).eq("qua_type",Const.DOCUMENT_QUA_TYPE.QUA_PATROL_RESULT));
            this.saveDocumentPatrolResultList(quaPatrolDTO);
        }
    }

    @Override
    public void submit(QuaPatrolDTO quaPatrolDTO) {
        // 暂存有新增和编辑场景
        if (quaPatrolDTO.getId() == null) {
            quaPatrolDTO.setPatrolStatus(Const.UPKEEP_STATUS_WAIT);
            sysDocumentNumService.incrementLastSerialByDocumentId(Const.DOCUMENT_ID.QUA_PATROL_ORDER_DOCUMENT);
            quaPatrolMapper.insert(quaPatrolDTO);
            // 解析执行设备项目关系表
            if (quaPatrolDTO.getQuaPatrolDetailList().size() > 0) {
                this.dealList(quaPatrolDTO);
            } else {
                throw new BDException("操作失败，不包含巡检标准集合！");
            }
            // 巡检文件操作
            this.saveDocumentPatrolList(quaPatrolDTO);
            // 巡检文件操作
            this.saveDocumentPatrolResultList(quaPatrolDTO);
        } else {
            if (quaPatrolDTO.getQuaPatrolDetailList().size() > 0) {
                quaPatrolDTO.setPatrolStatus(Const.UPKEEP_STATUS_WAIT);
                detailService.remove(new QueryWrapper<QuaPatrolDetail>().eq("patrol_id", quaPatrolDTO.getId()));
                this.dealList(quaPatrolDTO);
            } else {
                throw new BDException("操作失败，不包含设备巡检标准集合！");
            }
            quaPatrolMapper.updateById(quaPatrolDTO);
            // 质量巡检-拍照签到附件
            documentService.remove(new QueryWrapper<QuaDocument>().eq("qua_id", quaPatrolDTO.getId()).eq("qua_type",Const.DOCUMENT_QUA_TYPE.QUA_PATROL));
            this.saveDocumentPatrolList(quaPatrolDTO);
            // 质量巡检-巡检结果附件
            documentService.remove(new QueryWrapper<QuaDocument>().eq("qua_id", quaPatrolDTO.getId()).eq("qua_type",Const.DOCUMENT_QUA_TYPE.QUA_PATROL_RESULT));
            this.saveDocumentPatrolResultList(quaPatrolDTO);
        }
    }

    @Override
    public void finish(QuaPatrolDTO quaPatrolDTO) {
        if (quaPatrolDTO.getQuaPatrolDetailList().size() > 0) {
            quaPatrolDTO.setPatrolStatus(Const.UPKEEP_STATUS_OVER);
            detailService.remove(new QueryWrapper<QuaPatrolDetail>().eq("patrol_id", quaPatrolDTO.getId()));
            this.dealList(quaPatrolDTO);
        } else {
            throw new BDException("操作失败，不包含巡检标准集合！");
        }
        quaPatrolMapper.updateById(quaPatrolDTO);
        // 质量巡检-巡检结果附件
        documentService.remove(new QueryWrapper<QuaDocument>().eq("qua_id", quaPatrolDTO.getId()).eq("qua_type",Const.DOCUMENT_QUA_TYPE.QUA_PATROL_RESULT));
        this.saveDocumentPatrolResultList(quaPatrolDTO);
    }

    @Override
    public QuaPatrolVO getByInfoById(Integer id) {
        QuaPatrol quaPatrol = quaPatrolMapper.selectById(id);
        // 组装负责人姓名，电话，部门
        SysUser user = userService.getById(quaPatrol.getPatrolPersonId());
        if (user != null) {
            quaPatrol.setPlanPersonName(user.getRealName());
            quaPatrol.setPlanPersonDept(deptService.getById(user.getDeptId()).getDeptName());
            quaPatrol.setPlanPersonTel(user.getMobile());
        }
        QuaPatrolVO quaPatrolVO = new QuaPatrolVO();
        BeanUtil.copyProperties(quaPatrol, quaPatrolVO);
        // 组装巡检明细
        List<QuaPatrolDetail> patrolDetailList = detailService.list(new QueryWrapper<QuaPatrolDetail>().eq("patrol_id",id));
        if (patrolDetailList.size() > 0) {
            for (int i = 0; i < patrolDetailList.size(); i++) {
                // 注入名称标准上下限
                QuaPatrolDetail quaPatrolDetail = patrolDetailList.get(i);
                QuaPatrolStandard quaPatrolStandard = standardService.getById(quaPatrolDetail.getPatrolStandardId());
                patrolDetailList.get(i).setStandardName(quaPatrolStandard.getStandardName());
                patrolDetailList.get(i).setStandardValue(quaPatrolStandard.getStandardValue());
                patrolDetailList.get(i).setStandardMin(quaPatrolStandard.getStandardMin());
                patrolDetailList.get(i).setStandardMax(quaPatrolStandard.getStandardMax());
            }
            quaPatrolVO.setQuaPatrolDetailList(patrolDetailList);
        }
        // 组装图片1
        List<QuaDocument> list = documentService.list(new QueryWrapper<QuaDocument>().eq("qua_id", id).
                eq("qua_type",Const.DOCUMENT_QUA_TYPE.QUA_PATROL));
        List<Integer> fileIds = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            fileIds.add(list.get(i).getDocumentId());
        }
        if (list.size() > 0) {
            List<SysFileHis> sysFileHis = fileHisService.listByIds(fileIds);
            quaPatrolVO.setSysFileHisList(sysFileHis);
        }
        // 组装图片2
        List<QuaDocument> imgList = documentService.list(new QueryWrapper<QuaDocument>().eq("qua_id", id).
                eq("qua_type",Const.DOCUMENT_QUA_TYPE.QUA_PATROL_RESULT));
        List<Integer> imgFileIds = new ArrayList<>();
        for (int i = 0; i < imgList.size(); i++) {
            imgFileIds.add(imgList.get(i).getDocumentId());
        }
        if (list.size() > 0) {
            List<SysFileHis> sysFileHis = fileHisService.listByIds(imgFileIds);
            quaPatrolVO.setResultFileHisList(sysFileHis);
        }
        return quaPatrolVO;
    }

    private void dealList(QuaPatrolDTO quaPatrolDTO) {
        for (int i = 0; i < quaPatrolDTO.getQuaPatrolDetailList().size(); i++) {
            QuaPatrolDetail detail = new QuaPatrolDetail();
            BeanUtil.copyProperties(quaPatrolDTO.getQuaPatrolDetailList().get(i),detail);
            detail.setPatrolId(quaPatrolDTO.getId());
            detailService.save(detail);
        }
    }
    private void saveDocumentPatrolList(QuaPatrolDTO quaPatrolDTO) {
        if (quaPatrolDTO.getSysFileHisList() != null && quaPatrolDTO.getSysFileHisList().size() > 0) {
            List<QuaDocument> quaDocumentList = new ArrayList<>();
            for (int i = 0; i < quaPatrolDTO.getSysFileHisList().size(); i++) {
                QuaDocument quaDocument = new QuaDocument();
                quaDocument.setQuaId(quaPatrolDTO.getId());
                quaDocument.setDocumentId(quaPatrolDTO.getSysFileHisList().get(i).getId());
                quaDocument.setQuaType(Const.DOCUMENT_QUA_TYPE.QUA_PATROL);
                quaDocumentList.add(quaDocument);
            }
            documentService.saveBatch(quaDocumentList);
        }
    }

    private void saveDocumentPatrolResultList(QuaPatrolDTO quaPatrolDTO) {
        if (quaPatrolDTO.getSysFileHisList() != null && quaPatrolDTO.getResultFileHisList().size() > 0) {
            List<QuaDocument> quaDocumentList = new ArrayList<>();
            for (int i = 0; i < quaPatrolDTO.getResultFileHisList().size(); i++) {
                QuaDocument quaDocument = new QuaDocument();
                quaDocument.setQuaId(quaPatrolDTO.getId());
                quaDocument.setDocumentId(quaPatrolDTO.getResultFileHisList().get(i).getId());
                quaDocument.setQuaType(Const.DOCUMENT_QUA_TYPE.QUA_PATROL_RESULT);
                quaDocumentList.add(quaDocument);
            }
            documentService.saveBatch(quaDocumentList);
        }
    }
}




